#include <stdio.h>
#include <pthread.h>
#include <unistd.h>


pthread_mutex_t mutex;


void * threadFunc1(void *)
{
    printf("sub thread 1 is running\n");
    //子线程加锁
    pthread_mutex_lock(&mutex);

    printf("\nsub thread 1 lock success\n");

    return NULL;
}


int main()
{
    //互斥锁的初始化
    pthread_mutex_init(&mutex, NULL);

    pthread_t pthid;
    pthread_create(&pthid, NULL, threadFunc1, NULL);

    //主线程加锁
    pthread_mutex_lock(&mutex);


    printf("main thread lock success\n");

    sleep(3);

    //主线程解锁
    pthread_mutex_unlock(&mutex);

    pthread_join(pthid, NULL);


    printf("main thread is exiting\n");

    return 0;
}

